# THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),  
# to deal in the Software without restriction, including without limitation  
# the rights to use, copy, modify, merge, publish, distribute, sublicense,  
# and/or sell copies of the Software, and to permit persons to whom the  
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in 
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS IN THE SOFTWARE.

# intro.py
# written by: 
#     Jan Jokela <jan@devion.org>

import os, sys
import base.config.data as config_data
from qgl import scene
from fluid.eye import *
import fluid.actions as actions
import engines.audio.player as audioplayer

class Intro(object):
    def __init__(self):
        self.audio_player = audioplayer.Generic(player_driver='gstreamer')
        self.audio_player.play(config_data.audio + '/intro.mp3')
        self.welcome = Welcome()
        eventhandler.subscribe(self)
        eventhandler.subscribe(self.welcome)
    def handle_events(self, events):
        for event in events:
            if event.type is KEYDOWN:
                if event.key == K_SPACE:
                    self.welcome.destroy()
                    eventhandler.unsubscribe(self.welcome)
                    eventhandler.unsubscribe(self)
                    import base.menu as menu
                    self.menu = menu.MainMenu()
                    root_node.accept(compiler)
class Welcome(object):
    def __init__(self):
        self.draw()
    def draw(self):
        print config_data.fonts + "/Purisa.ttf"
        
        img = os.path.join(config_data.theme, 'intro_background.png')
        tex = qgl.scene.state.Texture(img)
        quad = qgl.scene.state.Quad((80,48))
        self._w_background = qgl.scene.Group()
        self._w_background.add(tex, quad)
        self._w_background.translate = (0,0,-50)
        viewport.add(self._w_background)
         
        self.welcomes = {
             0: ['Benvenuto', (-7,4,-210), (-1,-1,0)],
             1: ['Welcom', (-20,-10,-190), (-5,-2,0)],
             2: ['Bienvenido', (-20,10,-170), (-5,2,0)],
             3: ['Velkommen', (10,6,-150), (2,2,0)],
             4: ['Bem vindo', (-7,4,-130), (-1,-1,0)],
             5: ['Tervettuloa', (-20,-10,-110), (-5,-2,0)],
             6: ['Willkommen', (-20,10,-90), (-5,2,0)],
             7: ['Bienvenue', (10,6,-70), (2,2,0)],
             8: ['Welcome', (-7,4,-50), (-1,-1,0)]
                         }
        self._w_welcome = {}
        self._a_move = {}
        for k in self.welcomes:
            text = qgl.scene.state.Text(self.welcomes[k][0], config_data.fonts + "/URWGothicL.ttf", (0.8, 0.8, 0.8), None, 28)
            self._w_welcome[k]= qgl.scene.Group()
            self._w_welcome[k].add(text)
            self._w_welcome[k].translate = (self.welcomes[k][1])
            viewport.add(self._w_welcome[k])
            self._a_move[k] = actions.Move(self._w_welcome[k], self.welcomes[k][2], (-self.welcomes[k][1][2])*1.6/50)
            
        self.color = qgl.scene.state.Color((1.0,1.0,1.0,0.0))
        img = os.path.join(config_data.theme, 'devion_logo.png')
        tex = qgl.scene.state.Texture(img)
        quad = qgl.scene.state.Quad((13.93,3.45))
        self._w_logo = qgl.scene.Group()
        self._w_logo.add(tex, self.color, quad)
        self._w_logo.translate = (0,0,-24)
        viewport.add(self._w_logo)
        
        text = qgl.scene.state.Text('mediaplex', config_data.fonts + "/Purisa.ttf", (0.8, 0.8, 0.8), None, 28)
        self._w_mediaplex = qgl.scene.Group()
        self._w_mediaplex.add(self.color, text)
        self._w_mediaplex.translate = (3,-3.2,-23)
        viewport.add(self._w_mediaplex)
        self._a_fade = None
    def handle_events(self, events):
        for k in self._a_move:
            if not self._a_move[k].finished(): 
                self._a_move[k].execute()
        if self._a_move[0].finished():
            if not self._a_fade:
                self._a_fade = actions.Fade(self.color, alpha=1.0, time=5.0) 
            elif not self._a_fade.finished(): self._a_fade.execute() 
    def destroy(self):
        viewport.remove(self._w_background)
        del self._w_background
        viewport.remove(self._w_mediaplex)
        del self._w_mediaplex
        viewport.remove(self._w_logo)
        del self._w_logo
        for k in self.welcomes:
            viewport.remove(self._w_welcome[k])
        del self.welcomes